#include "brick.h"

CBrick::CBrick() : CObjects()
{

}

CBrick::CBrick(int Type,int Kind,int Contain, float posX,float posY)
{
	reality = 1;
	type = Type;
	kind = Kind;
	pos_x = posX;
	pos_y = posY;
	width = 16;
	height = 16;
	anim = 0;
	v_x = 0;
	v_y = 0;
	lowest = pos_y;
	contain = Contain;
}

CBrick::~CBrick()
{

}

void CBrick::Die()
{
	if(kind != 1) return;
	life = 0;
	int k = cTree->Del(pos_x,pos_y,width,height);
	if(pa1->life == 0 || pb1->life == 1)
	{
		pa1->Activate(pos_x,pos_y,0);
		pa2->Activate(pos_x,pos_y,1);
		pa3->Activate(pos_x,pos_y,2);
		pa4->Activate(pos_x,pos_y,3);
	}
	else
	{
		pb1->Activate(pos_x,pos_y,0);
		pb2->Activate(pos_x,pos_y,1);
		pb3->Activate(pos_x,pos_y,2);
		pb4->Activate(pos_x,pos_y,3);	
	}

}

void CBrick::Hitted(CObjects *obj)
{
	if(kind == 1 || kind == 4)
	{
		v_y = -0.2f;
		if(contain == 1)
		{
			if(obj->lv == 0)
				mushroom->Activate(1,0,pos_x,pos_y);
			else
				mushroom->Activate(0,0,pos_x,pos_y);
			kind = 2;
		}
		else if(contain == 2)
		{
			star->Activate(pos_x,pos_y);
			kind = 2;
		}
		else if(contain > 10)
		{
			jumpingCoin->Activate(pos_x,pos_y);
			contain--;
			obj->CoinGot++;
			if(contain == 10) 
			{
				kind = 2;
				contain = 0;
			}
		}
	}
	if(kind == 1 && obj->lv > 0 && contain == 0)
	{
		Die();
	}
}

void CBrick::Update(int t)
{
	pos_x += v_x*t;
	pos_y += v_y*t;
	if(pos_y < lowest)
		v_y += 0.1f;
	if(pos_y > lowest)
	{
		v_y = 0;
		pos_y = lowest;
	}
}

void CBrick::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	DWORD now = GetTickCount();
	Update(t);
	if ((now - last_time) > 100)
	{
		anim=(anim+1) % 3;
		last_time = now;
	}
	SpriteHandlerSP->Begin(D3DXSPRITE_ALPHABLEND);
	if(kind == 0)		// nen`
	{
		if(type == 1)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,0,0);
		else if(type == 2)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,2,5);
	}
	else if(kind == 1) // gach
	{
		if(type == 1)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,0,5);
		else if(type == 2)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,2,4);
	}
	else if (kind == 2) // gach cung
	{
		if(type == 1)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,0,6);
		else if(type == 2)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,3,7);
	}
	else if(kind == 3) // da bac thang
	{
		if(type == 1)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,2,2);
		else if(type == 2)
			StaticPic->Render(_BackBuffer,pos_x,pos_y,3,5);

	}
	else if(kind == 4) // dau ?
	{
		StaticPic->Render(_BackBuffer,pos_x,pos_y,9,anim+8);
	}
	SpriteHandlerSP->End();
}